package org.xqyy.eval.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xqyy.eval.dao.TDeptMapperMapper;
import org.xqyy.eval.dao.TDeptmapperPaperMapper;
import org.xqyy.eval.dao.TPaperMapper;
import org.xqyy.eval.model.*;
import org.xqyy.eval.service.IDeptMapperService;
import org.xqyy.eval.service.IEvalMasterService;
import org.xqyy.eval.service.ISystemCycleService;
import org.xqyy.eval.service.ITestPaperService;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
@Transactional
public class DeptMapperService implements IDeptMapperService {

    @Autowired
    private TDeptMapperMapper tDeptMapperMapper;
    @Autowired
    private TDeptmapperPaperMapper tDeptmapperPaperMapper;
    @Autowired
    private TPaperMapper paperMapper;
    @Autowired
    private ISystemCycleService systemCycleService;
    @Autowired
    private ITestPaperService paperService;
    @Autowired
    private IEvalMasterService evalMasterService;
    /**
     * 添加科室评价对照信息
     * @param tDeptMapper
     * @return
     */
    @Override
    public boolean addDeptMapper(TDeptMapper tDeptMapper) {
        if (tDeptMapperMapper.insert(tDeptMapper) > 0 ){
            return true;
        }
        return false;
    }

    /**
     * 添加科室评价对照信息
     *
     * @param dept
     * @param evalDepts 可以评价的科室集合
     * @return
     */
    @Override
    public boolean addDeptMapper(TDept dept, List<TDept> evalDepts) {
        for (TDept evalDept : evalDepts) {
            TDeptMapper tDeptMapper = new TDeptMapper();
            tDeptMapper.setDeptid(dept.getId());
            tDeptMapper.setEvaldeptid(evalDept.getId());
            addDeptMapper(tDeptMapper);
        }
        return true;
    }

    /**
     * 查询该科室应当评价的科室集合
     *
     * @param dept
     * @return
     */
    @Override
    public List<String> findEvalDeptList(TDept dept) {
        TDeptMapper tDeptMapper = new TDeptMapper();
        tDeptMapper.setDeptid(dept.getId());
        List<TDeptMapper> deptMappers = tDeptMapperMapper.select(tDeptMapper);
        List<String> evalDeptIdList = new ArrayList<String>();
        for (TDeptMapper evaldept : deptMappers) {
            evalDeptIdList.add(evaldept.getEvaldeptid());
        }
        return evalDeptIdList;
    }

    /**
     * 查询该科室应当评价的科室集合
     * @param dept
     * @return
     */
    public List<TDept> findEvalDeptListByDeptid(TDept dept){
        TDeptmapperPaper deptmapperPaper = new TDeptmapperPaper();
        deptmapperPaper.setDeptid(dept.getId());
        return tDeptmapperPaperMapper.selectdeptmapperpaper(deptmapperPaper);
    }

    /**
     * @param dept      科室信息
     * @param evalDepts 可以评价的科室集合
     * @return
     */
    @Override
    public boolean updateDeptMapper(TDept dept, List<TDept> evalDepts) {
        TDeptMapper tDeptMapper = new TDeptMapper();
        tDeptMapper.setDeptid(dept.getId());
        tDeptMapperMapper.delete(tDeptMapper);
        return addDeptMapper(dept,evalDepts);
    }

    /**
     * 拷贝科室评价关系
     *
     * @param copydept    参考科室
     * @param copyToDepts 进行评价绑定的科室
     * @return
     */
    @Override
    public boolean copyDeptMapper(TDept copydept, List<TDept> copyToDepts) {
        List<String> copyEvalDeptIdList = findEvalDeptList(copydept);
        List<TDept> copyEvalDeptList = new ArrayList<>();
        for (String copyEvalDeptId : copyEvalDeptIdList) {
            TDept tDept = new TDept();
            tDept.setId(copyEvalDeptId);
            copyEvalDeptList.add(tDept);
        }
        for (TDept copyToDept : copyToDepts) {
            updateDeptMapper(copyToDept,copyEvalDeptList);
        }

        List<String> copyToDeptIdList = new ArrayList<>();
        for (TDept copyToDept : copyToDepts) {
            copyToDeptIdList.add(copyToDept.getId());
        }
        paperService.copyEvalDeptPaper(copydept.getId(),copyToDeptIdList);
        return true;
    }


    /**
     * 绑定评价科室应当使用的试卷
     * @param dept 评价科室
     * @param evalDepts 被评价科室列表
     * @param paperId 试卷id
     * @return
     */
    public boolean updateDeptMapperPaper(TDept dept,List<TDept> evalDepts,Integer paperId){
        TDeptmapperPaper deptmapperPaper = null;

        for (TDept evalDept : evalDepts) {
            deptmapperPaper = new TDeptmapperPaper();
            deptmapperPaper.setDeptid(dept.getId());
            deptmapperPaper.setEvaldeptid(evalDept.getId());
            Example example = new Example(TDeptmapperPaper.class);
            example.createCriteria().andEqualTo("deptid",deptmapperPaper.getDeptid())
                    .andEqualTo("evaldeptid",deptmapperPaper.getEvaldeptid());
            if (tDeptmapperPaperMapper.selectCount(deptmapperPaper) > 0){
                deptmapperPaper.setPapeid(paperId);
                tDeptmapperPaperMapper.updateByExample(deptmapperPaper,example);
            }else{
                deptmapperPaper.setPapeid(paperId);
                tDeptmapperPaperMapper.insert(deptmapperPaper);
            }


        }
        return true;
    }

    /**
     * 根据科室id查询该科室应当评价的科室及其关联的
     * 调查问卷和问题
     *
     * @param tDept 要查询的科室信息
     * @return
     */
    @Override
    public List<TDept> selectEvalDeptPaperByDeptId(TDept tDept) {
        return tDeptmapperPaperMapper.selectEvalDeptPaperByDeptId(tDept);
    }

    /**
     * 查询科室本月待填报问卷
     * @param deptCode 科室code
     * @return
     */
    public  List<TestPaper> selectDeptUpcomingPaper(String deptCode){
        List<TestPaper> resultPaperList = new ArrayList<>();
        if (systemCycleService.nowIsPresenceCycle()){
            TSystemCycle systemCycle =  systemCycleService.selectNowDate();
            List<TDeptmapperPaper> deptmapperPaperList = gettDeptmapperPapers(deptCode);
            for (TDeptmapperPaper deptmapperPaper : deptmapperPaperList) {
                if (!evalMasterService.evalMasterIsPresence(deptCode,deptmapperPaper.getPapeid(),systemCycle.getCycledate())){
                    TestPaper testPaper =  paperMapper.selectByPrimaryKey(deptmapperPaper.getPapeid());
                    testPaper.setEvalDeptNameString(findEvalDeptNameByDeptId(deptCode,testPaper.getPaperId() ));
                    testPaper.setCycledate(systemCycle.getCycledate());
                    resultPaperList.add(testPaper);
                }

            }
        }
        return resultPaperList;
    }

    /**
     * 查询部门应当填报多种调查问卷（过滤重复调查问卷）
     * @param deptid 科室id
     * @return
     */
    private List<TDeptmapperPaper> gettDeptmapperPapers(String deptid) {
        Example deptmapperPaperExample = new Example(TDeptmapperPaper.class);
        deptmapperPaperExample.selectProperties("papeid");
        deptmapperPaperExample.createCriteria().andEqualTo("deptid",deptid);
        deptmapperPaperExample.setDistinct(true);
        return tDeptmapperPaperMapper.selectByExample(deptmapperPaperExample);
    }

    /**
     * 根据科室id查询待评价科室名称字符
     *
     * @param deptId 科室id
     * @param paperId  试卷id
     * @return
     */
    @Override
    public String findEvalDeptNameByDeptId(String deptId, Integer paperId) {
        HashMap<String,Object> map = paperMapper.findEvalDeptNameByDeptId(deptId,paperId);
        return (String)map.get("evaldeptname");
    }

    /**
     * 根据科室id查询待评价科室名称字符
     * @param deptId 科室id
     * @param paperId  试卷id
     * @return
     */
    @Override
    public  List<TDept> findEvalDeptListByDeptId(String deptId,Integer paperId){
            return paperMapper.findEvalDeptListByDeptId(deptId,paperId);
    }
}
